<?php
class CmsAction extends BaseAction{

	public function index(){
		//过滤
		$map = array();

		//ID
		if(isset($_POST['id'])&&!empty($_POST['id'])) {
			$map['id'] = $_POST['id'];
		}
		//标题
		if(isset($_POST['title'])&&!empty($_POST['title'])) {
			$map['title'] = array("like","%".$_POST['title']."%");
		}
		//用户ID
		if(isset($_POST['userId'])&&!empty($_POST['userId'])) {
			$map['userId'] = $_POST['userId'];
		}

		$Model = D('Cms');

		//排序字段 默认为主键名
		if(isset($_REQUEST['order'])) {
			$order = $_REQUEST['order'];
		}else {
			$order = !empty($sortBy)? $sortBy: $Model->getPk();
		}

		//排序方式默认按照倒序排列
		//接受 sost参数 0 表示倒序 非0都 表示正序
		if(isset($_REQUEST['sort'])) {
			$sort = $_REQUEST['sort']?'asc':'desc';
		}else {
			$sort = $asc?'asc':'desc';
		}

		//$list = $Model->where($map)->findAll();
		$list = $Model ->where($map)->order($order.' '.$sort)->findPage();

		//列表排序显示
		$sortImg    = $sort ;                                   //排序图标
		$sortAlt    = $sort == 'desc'?'升序排列':'倒序排列';    //排序提示
		$sort       = $sort == 'desc'? 1:0;                     //排序方式
		$this->assign('sort',       $sort);
		$this->assign('order',      $order);
		$this->assign('sortImg',    $sortImg);
		$this->assign('sortType',   $sortAlt);

		$this->assign("count",$list['count']);
		$this->assign("page",$list['html']);
		$this->assign("list",$list['data']);

		$this->display();
	}
	//添加文章分类
	function addCategory() {
		$tree = D('CmsCate')->makeTree(0, 1);
		$tree	=	json_encode($tree);
		$this->assign('category_json',$tree);

		$categoryList	=	D('CmsCate')->makeLiTree($pid);
		$this->assign('categoryList',$categoryList);

		$this->display();
	}

	//添加文章分类
	function doAddCategory() {
		$cate['name']	=	t($_POST['name']);
		$cate['title']	=	t($_POST['title']);
		$cate['pid']	=	D('CmsCate')->digCate($_POST);
		$cate['addcontent_tpl']=	t($_POST['addcontent_tpl']);
		$cate['content_tpl']=	t($_POST['content_tpl']);
		$cate['category_tpl']=	t($_POST['category_tpl']);
		$categoryId = D('CmsCate')->add($cate);
		if($categoryId){
			//增加操作日志
			addLog($this->mid, array('title'=>$cate['title']), 'cms_cate_add');

			U('admin/Cms/addCategory', '', true);
		}else{
			$this->error('分类添加失败！');
		}
	}
	//添加文章内容
	function addContent() {
		$tree = D('CmsCate')->makeTree(0);
		$tree	=	json_encode($tree);
		$this->assign('category_json',$tree);

		$this->display();
	}
	function addP() {
		$tree = D('CmsCate')->makeTree(0);
		$tree	=	json_encode($tree);

		$this->assign('category_json',$tree);
		$this->display();
	}
	function addV() {
		$tree = D('CmsCate')->makeTree(0);
		$tree	=	json_encode($tree);

		$this->assign('category_json',$tree);
		$this->display();
	}
	function addM() {
		$tree = D('CmsCate')->makeTree(0);
		$tree	=	json_encode($tree);

		$this->assign('category_json',$tree);
		$this->display();
	}
	function addD() {
		$tree = D('CmsCate')->makeTree(0);
		$tree	=	json_encode($tree);

		$this->assign('category_json',$tree);
		$this->display();
	}
	//添加文章内容
	function doAddContent() {
		$map['content']		=	addslashes($_POST['content']);
		$map['title']		=	t($_POST['title']);
		$map['url']			=	t($_POST['url']);

		$map['author']		=	t($_POST['author']) ? t($_POST['author']) : '管理员';
		$map['category']	=	D('CmsCate')->digCate($_POST);
		$map['publish_time']=	time();
		$map['user_id']		=   $_SESSION['mid'];
		$map['cate_tree']	=	D('CmsCate')->digCateTree($_POST);

		//上传图片
		//$path	=	'./Public/Uploads/CMS/'.date('Y-m-d',time()).'/';
		//checkDir($path);
		//$info	=	$this->_upload($path);
		//$map['img']	=	$info[0]['savepath'].$info[0]['savename'];
		$contentId	=	D('Cms')->add($map);
		if($contentId){
			//增加操作日志
			addLog($this->mid, array('title'=>$map['title']), 'cms_add');

			U('admin/Cms/index', '', true);
		}else{
			$this->error('内容添加失败！');
		}
	}

	//修改分类
	function editCategory() {
		$id	=	intval($_REQUEST['id']);
		$this->assign('id', $id);

		if(isset($_POST['editSubmit'])) {
			if(!$id) $this->error('错误的分类ID！');
			if(empty($_POST['title'])){
				$this->error('名称不能为空！');
			}

			$cate['title']	=	t($_POST['title']);
			//$cate['pid']	=	$this->Cagetory->_digCate($_POST);  //多级分类需要处理开始

			$pid = $cate['pid'] = intval($_POST['pid0']);  //1级分类

			//$pid	=	intval(D('CmsCate')->digCate($_POST));
			if(!$pid){
				$this->error('分类必须选！');
			}
			if($pid==$id){
				$categoryId = D('CmsCate')->setField('title',$cate['title'],'id='.$id);
			}else{
				$categoryId = D('CmsCate')->where("id='$id'")->save($cate);
			}


			if($categoryId){
				addLog($this->mid, array('title'=>$cate['title']), 'cms_cate_edit');
				
				U('admin/Cms/addCategory', '', true);
			}else{
				$this->error('操作失败！');
			}
		}
		$vo	= D('CmsCate')->where("id=$id")->find();
		//$tree	=	json_encode(D('CmsCate')->makeTree(0));

		//$this->assign('category_json',$tree);
		$this->assign('vo',$vo);
		$this->display();
	}

	//删除分类
	function delCategory() {
		$id	=	intval($_REQUEST['id']);
		if(D('CmsCate')->where('id='.$id)->delete()){
			addLog($this->mid, '', 'cms_cate_del');
			
			U('admin/Cms/addCategory', '', true);
		}else{
			$this->error('删除失败！');
		}

	}

	function edit() {
		$model = D('Cms');
		$id = $_REQUEST['id'];
		$vo	=	$model->where('id='.$id)->find();

		$this->assign('vo',$vo);

		$tree	=	json_encode(D('CmsCate')->makeTree(0));
		$this->assign('category_json',$tree);

		$this->display();
	}

	function update() {
		$model	= D('Cms');
		$vo['title'] = t($_POST['title']);
		$vo['content'] = h($_POST['content']);
		$vo['url'] = h($_POST['url']);
		$vo['author'] = h($_POST['author']);
		$vo['category']	=	D('CmsCate')->digCate($_POST);
		$vo['user_id']		=   $_SESSION['mid'];
		$vo['cate_tree']	=	D('CmsCate')->digCateTree($_POST);

		$id = intval($_POST['id']);
		$res = $model->where("id='$id'")->save($vo);

		if($res) {
			//增加操作日志
			addLog($this->mid, array('title'=>$vo['title']), 'cms_edit');

			//成功提示
			$url = U('admin/Cms/index', '', true);
		}else {
			//错误提示
			$this->error("更新失败! ".$model->getError());
		}
	}

	/**
		 +----------------------------------------------------------
		 * 默认删除操作
		 +----------------------------------------------------------
		 * @access public
		 +----------------------------------------------------------
		 * @return string
		 +----------------------------------------------------------
		 * @throws ThinkExecption
		 +----------------------------------------------------------
		 */
	public function delete()
	{
		//删除指定记录
		$model = D('Cms');
		if(!empty($model)) {
			$id = $_REQUEST['id'];
			if(isset($id)) {
				if($model->where('id In('.$id.')')->delete()){
					//增加操作日志
					addLog($this->mid, '', 'cms_del');

					$url = U('admin/Cms/index', '', true);
				}else {
					$this->error("删除失败!");
				}
			}else {
				$this->error('非法操作');
			}
		}
	}

	/**
		 +----------------------------------------------------------
		 * 默认禁用操作
		 *
		 +----------------------------------------------------------
		 * @access public
		 +----------------------------------------------------------
		 * @return string
		 +----------------------------------------------------------
		 * @throws FcsException
		 +----------------------------------------------------------
		 */
	function forbid()
	{
		$model	=	D('Cms');
		$condition = 'id IN ('.$_GET['id'].')';
		if($model->where($model->setField('status','0', $condition))){
			U('admin/Cms/index', '', true);
		}else {
			$this->error('状态禁用失败');
		}
	}

	/**
		 +----------------------------------------------------------
		 * 默认恢复操作
		 *
		 +----------------------------------------------------------
		 * @access public
		 +----------------------------------------------------------
		 * @return string
		 +----------------------------------------------------------
		 * @throws FcsException
		 +----------------------------------------------------------
		 */
	function resume()
	{
		//恢复指定记录
		$model	=	D('Cms');
		$condition = 'id IN ('.$_GET['id'].')';

		if($model->where($model->setField('status','1', $condition))){
			U('admin/Cms/index', '', true);
		}else {
			$this->error('状态恢复失败');
		}
	}



	function setnew()
	{
		$model	=	D('Cms');
		$condition = 'id IN ('.$_GET['id'].')';
		if($model->setField('newicon','1',$condition)){
			U('admin/Cms/index', '', true);
		}else {
			$this->error('文章置新失败');
		}
	}

	function unsetnew()
	{
		//恢复指定记录
		$model	=	D('Cms');
		$condition = 'id IN ('.$_GET['id'].')';
		if($model->setField('newicon','0',$condition)){
			U('admin/Cms/index', '', true);
		}else {
			$this->error('文章恢复失败');
		}
	}

}
?>